package Twitter
{
	import DataClasses.ExtendedUserInfo;
	
	import mx.collections.XMLListCollection;
	
	public class Actions
	{
		import mx.core.Application;
		import mx.collections.SortField;
		import mx.collections.Sort;
		import mx.collections.ArrayCollection;
		import mx.rpc.events.ResultEvent;
		import mx.managers.CursorManager;
		import mx.utils.Base64Encoder;
		import mx.events.ValidationResultEvent;
		import mx.controls.Alert;
		import mx.utils.ObjectUtil;
		
		import mx.controls.DateField; 
		
		
		import flash.data.SQLConnection;
		import flash.data.SQLStatement;
		import flash.filesystem.File;
		import flash.events.*;
		
		import flash.net.URLLoader;
    	import flash.net.URLLoaderDataFormat;
    	import flash.net.URLRequest;
    	import flash.net.URLVariables;	
    	import flash.net.URLRequestHeader;
		
		import flash.events.SecurityErrorEvent;
		

		import tStatus;
		import tMessageFormat;
		

		///Lock actions
		public var LOCK_get_status:Boolean = false;
		
		
		
		public function Actions()
		{
		}
		
		public function getTimeline():void
		{
			
			
				var reqStr:String = "http://twitter.com/statuses/friends_timeline.xml?count="+Application.application._default_friends_count;
				
				if (Application.application.last_friend_status_id!="0")
				{
					
					reqStr += "&since_id="+Application.application.last_friend_status_id
				}
				
				//trace.show(reqStr);
				
				var request:URLRequest = new URLRequest(reqStr);
				
				
				var twitterPassword:Base64Encoder = new Base64Encoder();
				twitterPassword.encode(Application.application._username + ":" + Application.application._password);
				
				var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));
			
				var loader:URLLoader = new URLLoader();
				
				// this particular API requires a verb of GET instead of POST.
				request.requestHeaders = restHeader;
				request.method = "GET";
				
				request.authenticate = false;
				
				loader.addEventListener(Event.COMPLETE, gotTimeline);
				
				configureListeners(loader);
				loader.load(request);
				
				loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpStatusHandler);
				loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);


				CursorManager.setBusyCursor();
				
				

			
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
                dispatcher.addEventListener(Event.COMPLETE, completeHandler);
                dispatcher.addEventListener(Event.OPEN, openHandler);
                dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            }

            private function completeHandler(event:Event):void {
                var loader:URLLoader = URLLoader(event.target);
                trace("completeHandler: " + loader.data);
            }

            private function openHandler(event:Event):void {
                trace("openHandler: " + event.toString());
            }

            private function progressHandler(event:ProgressEvent):void {
                trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
            }

            private function securityErrorHandler(event:SecurityErrorEvent):void {
                //Alert.show("securityErrorHandler: " + event.toString());
            }

            private function httpStatusHandler(event:HTTPStatusEvent):void {
                trace("httpStatusHandler: " + event.toString());
            }

            private function ioErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event.toString());
                CursorManager.removeBusyCursor();
                
                //if (_logged_in = false)
                //{
                	//Alert.show("Could not Log into Twitter");
                	
                //}
            }    
		
		public function gotTimeline(e:Event):void
		{
		
					
			var myHTML:URLLoader = URLLoader(e.target);					
			var myXML:XMLList= XMLList(myHTML.data);			
			var getfirst:Boolean = true;
		
			var tf:tMessageFormat;
			
			var sort:Sort = new Sort();
			
			try
			{
			
				Application.application.db_prefs.db_keywords();
			}
			catch(e:Error)
			{
				
			}
			
			//Alert.show("timeline received" + );
			
			var myXMLCol:XMLListCollection = new XMLListCollection(myXML..status);
			
			var nsort:Sort = new Sort();
			nsort.fields = [new SortField("id", false, false, true)];
			
			myXMLCol.sort = nsort;
			myXMLCol.refresh();

			if (LOCK_get_status==false)
			{
			LOCK_get_status = true;
			
			//Alert.show("timeline received" + myXMLCol.length);
			
			for each (var item:XML in myXMLCol)
			{
				/*if (getfirst==true)
				{
					Application.application.last_friend_status_id = item.id.toString();
					getfirst = false;
					
				}*/
				if (uint(item.id.toString())>uint(Application.application.last_friend_status_id))
				{
				
				tf  = new tMessageFormat();
				
				var o:tStatus = new tStatus();
				o.id = uint(item.id.toString());
				
				//Alert.show(item.id.toString());
				
				//o.created_at = DateField.stringToDate(item.created_at.toString(), "DD-MM-YYYY");
				
				o.created_at = tf.format_date(item.created_at.toString());
				
				o.text = tf.format_url_in_message(item.text.toString());
				o.screen_name = item.user.screen_name.toString();
				o.profile_image_url = item.user.profile_image_url.toString();
				o._contains_url = tf._contains_hyperlink;
				o.user_id = item.user.id.toString();
				o.in_reply_to_screen_name = item.in_reply_to_screen_name.toString();
				o.in_reply_to_status_id = item.in_reply_to_status_id.toString();
				o.in_reply_to_user_id = item.in_reply_to_user_id.toString();
				
				
				//Alert.show(item.in_reply_to_status_id.toString());
				
				o._key_match = tf.match_keywords(item.text.toString());
				
				//
				 
				//myStatuses.addItemAt(o,0);
				/*if (Application.application._is_first_update)
				{
					Application.application.myStatuses.addItem(o);

					
				}
				else
				{*/
					Application.application.myStatuses.addItemAt(o,0);
				//}
				
				
				
				//addStatusToDatabase(o);
				
				Application.application.last_friend_status_id = item.id.toString();
				}
				}
			}
			LOCK_get_status = false;
			
			//sort = new Sort();
			//sort.fields = [new SortField("id", false, true)];
			//Application.application.myStatuses.sort = sort;
			


			CursorManager.removeBusyCursor();
			

			
			
		}
		

		
		public function get_direct_messages():void
		{
			
			var reqStr:String = "http://twitter.com/direct_messages.xml";
				
			if (Application.application.last_dm_status_id!="0")
			{		
				reqStr += "?since_id="+Application.application.last_dm_status_id
			}
			
			var request:URLRequest = new URLRequest(reqStr);
			
			
			var twitterPassword:Base64Encoder = new Base64Encoder();
			twitterPassword.encode(Application.application._username+ ":" + Application.application._password);
			var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));
		
			var loader:URLLoader = new URLLoader();
			
			request.requestHeaders = restHeader;
			request.method = "GET";

			
			loader.addEventListener(Event.COMPLETE, update_direct_message_complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, update_direct_message_ErrorHandler);
			loader.load(request);
			
			CursorManager.setBusyCursor();	
		}
		
		public function update_direct_message_complete(e:Event):void
		{
			// this is the function that will be called when we get data back from our
			// timeline update.  
			
			Application.application.viewstack1.selectedIndex = 1;
		
			var myHTML:URLLoader = URLLoader(e.target);
			
		
			var myXML:XMLList= XMLList(myHTML.data);

			var myXMLCol:XMLListCollection = new XMLListCollection(myXML..direct_message);
			
			var nsort:Sort = new Sort();
			nsort.fields = [new SortField("id", false, false, true)];
			
			myXMLCol.sort = nsort;
			myXMLCol.refresh();

			
			var getfirst:Boolean = true
			
		
			//myStatuses = new ArrayCollection();
			
			
			var tf:tMessageFormat;
			
			var sort:Sort = new Sort();
			
			for each (var item:XML in myXMLCol)
			{
				
				

				
				tf  = new tMessageFormat();
				
				var o:DirectMessages = new DirectMessages();
				o.id = int(item.id.toString());
				o.text = tf.format_url_in_message(item.text.toString());
				
				//o.created_at = DateField.stringToDate(item.created_at.toString(), "DD-MM-YYYY");
				

				
				o.created_at = tf.format_date(item.created_at.toString());
				
				//o.created_at = DateField.stringToDate(item.created_at.toString(),"DD-MM-YYYY" );
				
				o.sender_screen_name = item.sender_screen_name.toString();
				
				o.sender_profile_image_url = item.sender.profile_image_url.toString();


				Application.application.last_dm_status_id = item.id.toString();
				
				Application.application.myDirectMessages.addItemAt(o,0);
				
				sort = new Sort();
				sort.fields = [new SortField("id", false, true)];
				Application.application.myDirectMessages.sort = sort;
				
				//addStatusToDatabase(o);
				
				Application.application.last_dm_status_id = item.id.toString();
				

			}
			
			Application.application.counter.get_totals();
			
			

			CursorManager.removeBusyCursor();
			
		}
		
		private function update_direct_message_ErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event.toString());
                CursorManager.removeBusyCursor();
                
                //if (_logged_in = false)
                //{
                	//Alert.show("Could not get DM" + event.toString());
                	
                //}
            }
		
		
		public var last_id:String = "";
		public function sendTwitterStatus(status:String, is_scheduled:Boolean, id:String, in_reply_to_status_id:String, in_reply_to_user_id:String, in_reply_to_screen_name:String):void
		{
			
			last_id = id;
			/*var validatorResult:ValidationResultEvent = Application.application.vUpdateMessage.validate();
			if (validatorResult.type == ValidationResultEvent.INVALID) return;*/
			
			var request:URLRequest = new URLRequest("http://twitter.com/statuses/update.xml");
			
			var twitterPassword:Base64Encoder = new Base64Encoder();
			twitterPassword.encode(Application.application._username+ ":" + Application.application._password);
			var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));

			var message:URLVariables = new URLVariables();
			message.status = status;
			
			if (!is_scheduled)
			{
				message.in_reply_to_status_id = in_reply_to_status_id;
				message.in_reply_to_user_id = in_reply_to_user_id;
				message.in_reply_to_screen_name = in_reply_to_screen_name;
			}
			
			message.source = "RTweeter"
			
			var loader:URLLoader = new URLLoader();
			
			request.requestHeaders = restHeader;
			request.method = "POST";
			request.data = message;
			
			if (is_scheduled)
			{
				loader.addEventListener(Event.COMPLETE, scheduled_message_complete);
				loader.addEventListener(IOErrorEvent.IO_ERROR, failed_scheduled);
				try
				{
					loader.load(request);
					CursorManager.removeBusyCursor();
			
					//Delete Schedule
					Application.application.t_db.deleteScheduledTweet(id);
			
					Application.application.in_reply_to_screen_name = "";
           			Application.application.in_reply_to_status_id = "";
           		 	Application.application.in_reply_to_user_id = "";
				
				}
				catch(err:ReferenceError)
				{
					
					
					
				}
				
			}
			else
			{
				loader.addEventListener(Event.COMPLETE, update_message_complete);
				loader.addEventListener(IOErrorEvent.IO_ERROR, update_ErrorHandler);
				loader.load(request);
			}
			
			
			
			
			
			CursorManager.setBusyCursor();	
			
		}
		
		public function scheduled_message_complete(e:Event):void
		{
			CursorManager.removeBusyCursor();
			
			//Delete Schedule
			Application.application.t_db.deleteScheduledTweet(last_id);
			
			Application.application.in_reply_to_screen_name = "";
           	Application.application.in_reply_to_status_id = "";
            Application.application.in_reply_to_user_id = "";
		}
		
		public function failed_scheduled(event:IOErrorEvent):void
		{
			CursorManager.removeBusyCursor();
			for each(var o:ScheduledMessage in Application.application.ScheduledMessage)
			{
				if (o.id == (last_id))
				{
					o.sending == false;
					
				}
				
				
			}
			
			
		}
		
		private function update_ErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event.toString());
                CursorManager.removeBusyCursor();
                
                //if (_logged_in = false)
                //{
                	Alert.show("Could not send update");
                	
                //}
            } 
		
		public function update_message_complete(e:Event):void
		{
			CursorManager.removeBusyCursor();
			//Application.application.twitter_textbox.text = "";
		}
		

		

		
		
		
		public function get_replies():void
		{
			var reqStr:String = "http://twitter.com/statuses/replies.xml"
				
			if (Application.application.last_reply_status_id!="0")
			{
				
				reqStr += "?since_id="+Application.application.last_reply_status_id
			}
			
			//trace.show(reqStr);
			
			var request:URLRequest = new URLRequest(reqStr);
			
			
			var twitterPassword:Base64Encoder = new Base64Encoder();
			twitterPassword.encode(Application.application._username + ":" + Application.application._password);
			
			var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));
		
			var loader:URLLoader = new URLLoader();
			
			// this particular API requires a verb of GET instead of POST.
			request.requestHeaders = restHeader;
			request.method = "GET";
			
			request.authenticate = false;

			
			loader.addEventListener(Event.COMPLETE, get_replies_complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, get_replies_ErrorHandler);
			loader.load(request);
			
			CursorManager.setBusyCursor();	
		}
		
		public function get_replies_complete(e:Event):void
		{
			Application.application.viewstack1.selectedIndex = 1;		
			var myHTML:URLLoader = URLLoader(e.target);					
					
			var getfirst:Boolean = true;
		
			var tf:tMessageFormat;
			
			var sort:Sort = new Sort();
			
			var myXML:XMLList= XMLList(myHTML.data);

			var myXMLCol:XMLListCollection = new XMLListCollection(myXML..status);
			
			var nsort:Sort = new Sort();
			nsort.fields = [new SortField("id", false, false, true)];
			
			myXMLCol.sort = nsort;
			myXMLCol.refresh();

			
			
			
			try
			{
			
				Application.application.db_keywords();
			}
			catch(e:Error)
			{
				
			}
			
			for each (var item:XML in myXMLCol)
			{
				
				
				
				tf  = new tMessageFormat();
				
				var o:tStatus = new tStatus();
				o.id = int(item.id.toString());
				
				//o.created_at = DateField.stringToDate(item.created_at.toString(), "DD-MM-YYYY");
				
				o.created_at = tf.format_date(item.created_at.toString());
				
				o.text = tf.format_url_in_message(item.text.toString());
				o.screen_name = item.user.screen_name.toString();
				o.profile_image_url = item.user.profile_image_url.toString();
				o._contains_url = tf._contains_hyperlink;
				
				tf.match_keywords(item.text.toString());
				
				o._key_match = tf._matches_keyword;
				
				o.user_id = item.user.id.toString();
				o.in_reply_to_screen_name = item.in_reply_to_screen_name.toString();
				o.in_reply_to_status_id = item.in_reply_to_status_id.toString();
				o.in_reply_to_user_id = item.in_reply_to_user_id.toString();
				

				Application.application.myReplies.addItemAt(o,0);
				
				sort = new Sort();
				sort.fields = [new SortField("id", false, true)];
				Application.application.myStatuses.sort = sort;
				
				//addStatusToDatabase(o);
				Application.application.last_reply_status_id = item.id.toString();
				
			}
			

			
			//Alert.show("Replies = "+Application.application.myReplies.length);
			
			//var search:Search = new Search();
			//search.get_mentions_user();
			
			CursorManager.removeBusyCursor();
			
		}
		
		private function get_replies_ErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event.toString());
                CursorManager.removeBusyCursor();
                
                //if (_logged_in = false)
                //{
                	//Alert.show("Could not get Replies" + event.toString());
                	
                //}
            }
	
		///////////////////////////////////////
		/// FIRST TIME LOGIN
		///////////////////////////////////////
		
		public function login():void
		{
		
				
			var reqStr:String = "http://twitter.com/account/verify_credentials.xml";
				
				/*if (Application.application.last_friend_status_id!="0")
				{
					
					reqStr += "&since_id="+Application.application.last_friend_status_id
				}*/
				
				//trace.show(reqStr);
				
				var request:URLRequest = new URLRequest(reqStr);
				
				
				var twitterPassword:Base64Encoder = new Base64Encoder();
				twitterPassword.encode(Application.application._username + ":" + Application.application._password);
				
				var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));
			
				var loader:URLLoader = new URLLoader();
				
				// this particular API requires a verb of GET instead of POST.
				request.requestHeaders = restHeader;
				request.method = "GET";
				
				request.authenticate = false;
				
				loader.addEventListener(Event.COMPLETE, login_success);
				
				//configureListeners(loader);
				
				loader.addEventListener(IOErrorEvent.IO_ERROR, loginErrorHandler);
				loader.load(request);
				
				/*loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpStatusHandler);
				loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);*/


				CursorManager.setBusyCursor();
			
			
		}
		
		private function loginErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event.toString());
                CursorManager.removeBusyCursor();
                
                Application.application.viewstack1.visible = true;
                
                Application.application.viewstack1.selectedIndex = 0;
                
                //if (_logged_in = false)
                //{
                	Alert.show("Could not Log into Twitter");
                	
                //}
            }
		
		public function login_success(e:Event):void
		{
			
			// read prefs

			Application.application.viewstack1.selectedIndex = 1;
			/*Application.application.viewstack1.selectedIndex = 1;	
			
			Application.application.visible = true;		
			
			var myHTML:URLLoader = URLLoader(e.target);					
			var myXML:XML = XML(myHTML.data);			
			var getfirst:Boolean = true;
		
			var tf:tMessageFormat;
			
			var sort:Sort = new Sort();
			
			//try
			//{
				Application.application.t_db.getKeywords(Application.application._username);
				Application.application.db_prefs.db_keywords();
			//}
			//catch(e:Error)
			//{
				
			//}
			
			for each (var item:XML in myXML..status)
			{
				
				//Alert.show("Keywrods in first login - called" +Application.application.arr_keywords.length);
				if (getfirst==true)
				{
					Application.application.last_friend_status_id = item.id.toString();
					getfirst = false;
					
				}
				
				tf  = new tMessageFormat();
				
				var o:tStatus = new tStatus();
				o.id = int(item.id.toString());
				
				//o.created_at = DateField.stringToDate(item.created_at.toString(), "DD-MM-YYYY");
				
				o.created_at = tf.format_date(item.created_at.toString());
				
				o.text = tf.format_url_in_message(item.text.toString());
				o.screen_name = item.user.screen_name.toString();
				o.profile_image_url = item.user.profile_image_url.toString();
				o._contains_url = tf._contains_hyperlink;
				
				tf.match_keywords(item.text.toString());
				
				o._key_match = tf._matches_keyword;
				
				//
				 

				Application.application.myStatuses.addItem(o);

					


				
				
				
				//addStatusToDatabase(o);
				
				
			}
			
			sort = new Sort();
			sort.fields = [new SortField("id", false, true)];
			Application.application.myStatuses.sort = sort;
			
			*/
			CursorManager.removeBusyCursor();
			
			
			Application.application._logged_in = true;	
			// get panel details
			
			
			Application.application.timed_events.FirstTimeRun();
			
			rate_limit_status();
			
		}
		
		public function rate_limit_status():void
		{
			var reqStr:String = "http://twitter.com/account/rate_limit_status.xml";
				
		
				
				//trace.show(reqStr);
				
				var request:URLRequest = new URLRequest(reqStr);
				
				
				var twitterPassword:Base64Encoder = new Base64Encoder();
				twitterPassword.encode(Application.application._username + ":" + Application.application._password);
				
				var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));
			
				var loader:URLLoader = new URLLoader();
				
				// this particular API requires a verb of GET instead of POST.
				request.requestHeaders = restHeader;
				request.method = "GET";
				
				request.authenticate = false;
				
				loader.addEventListener(Event.COMPLETE, rate_limit_sucess);
				
				//configureListeners(loader);
				
				//loader.addEventListener(IOErrorEvent.IO_ERROR, loginErrorHandler);
				loader.load(request);
				
				/*loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpStatusHandler);*/
				loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);



		}
		
		public function rate_limit_sucess(e:Event):void
		{
			var myHTML:URLLoader = URLLoader(e.target);					
			var myXML:XML = XML(myHTML.data);	
			
			var hourly_limit:String = myXML["hourly-limit"];
			var remaining:String = myXML["remaining-hits"];
			var reset_seconds:String = myXML["reset-time-in-seconds"];
			
			var timeArr:Array = myXML["reset-time"].toString().split("T");
			
			var time:String = "";
			if (timeArr.length>0)
			{
				time = timeArr[1];
			}
			
			//public static const millisecondsPerMinute:int = 1000 * 60;
			//public static const millisecondsPerHour:int = 1000 * 60 * 60;
			
			var subArr:Array = time.split(":");
			
			var date:Date = new Date();
			
			var reset_min:int = int(subArr[1]);
			
			if (date.minutes>reset_min)
			{
				date.hours = date.hours + 1;
				
			}
			
			date.minutes = reset_min + 1;
			
			
			
			time = date.toTimeString();
			
			
			
			
			
			
			//var local_date:Date = new Date(myXML["reset-time"].toString());
			//time = myXML["reset-time"].toString().split("T");
			
			//time.milliseconds += int(reset_seconds);
			
			
			Application.application._account_api_limit= ("Limit: " + remaining + "/" +  hourly_limit + " Resets: "+time);
			
			
			
		}
		
		////////////////////////////////////////////////
		// Userdetails
		///////////////////////////////////////////////
		
		public function get_userdetails(screen_name:String):void
		{
			var reqStr:String = "http://twitter.com/users/show/"+screen_name+".xml";
				
			if (Application.application.last_dm_status_id!="0")
			{		
				reqStr += "?since_id="+Application.application.last_dm_status_id
			}
			
			var request:URLRequest = new URLRequest(reqStr);
			
			
			var twitterPassword:Base64Encoder = new Base64Encoder();
			twitterPassword.encode(Application.application._username+ ":" + Application.application._password);
			var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));
		
			var loader:URLLoader = new URLLoader();
			
			request.requestHeaders = restHeader;
			request.method = "GET";

			
			loader.addEventListener(Event.COMPLETE, userdetails_complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, userdetails_ErrorHandler);
			loader.load(request);
			
			//CursorManager.setBusyCursor();	
		}
		
		public function userdetails_complete(e:Event):void
		{
			// this is the function that will be called when we get data back from our
			// timeline update.  
			
		
			var myHTML:URLLoader = URLLoader(e.target);
			
		
			var myXML:XMLList= XMLList(myHTML.data);
			
			var user:ExtendedUserInfo = new ExtendedUserInfo();
			
			user.id = myXML.id;
			user.name = myXML.name;
			user.screen_name = myXML.screen_name;
			user.location = myXML.location;
			user.description= myXML.description;
			user.profile_image_url = myXML.profile_image_url;
			user.url = myXML.url;
			user.isprotected= myXML["protected"];
			user.followers_count = myXML.followers_count;
			user.friends_count = myXML.friends_count;
			user.following = myXML.following;
			user.statuses_count  = myXML.statuses_count;
			user.friends_count =  myXML.friends_count;
			
			user.status_created_at = myXML.status.created_at;
			user.status_text = myXML.status.text;
			
			Application.application.user_statuses = new ArrayCollection();
			
			if (myXML["protected"]!="false")
			{
				//getstatus
				
			}
			
			if (myXML.following!="false")
			{
				user.follow_status_option = "Unfollow"
			}
			else
			{
				user.follow_status_option = "Follow"
			}
			
			
			Application.application.extended_user = user;
			Application.application.viewstack2.selectedIndex=2;

			CursorManager.removeBusyCursor();
		}
		
		
		private function userdetails_ErrorHandler(event:IOErrorEvent):void {
			
		}
		
		public function follow_user(screen_name:String, follow:String):void
		{
			var reqStr:String;
			
			if (follow=="Follow")
			{
				reqStr = "http://twitter.com/friendships/create/"+screen_name+".xml";
			}
			else
			{
				reqStr = "http://twitter.com/friendships/destroy/"+screen_name+".xml";
			}
			
			reqStr = "http://twitter.com/friendships/create/rtweeter.xml";
				
			Alert.show(reqStr);
			
			var request:URLRequest = new URLRequest("http://twitter.com/friendships/create/rtweeter.xml");
			
			//request.authenticate = true;

			
			var twitterPassword:Base64Encoder = new Base64Encoder();
			twitterPassword.encode(Application.application._username+ ":" + Application.application._password);
			var restHeader:Array = new Array(new URLRequestHeader("Authorization", "Basic " + twitterPassword.toString()));

			//var message:URLVariables = new URLVariables();

			
			var loader:URLLoader = new URLLoader();
			
			
			
			request.requestHeaders = restHeader;
			request.method = "POST";


			
			loader.addEventListener(Event.COMPLETE, follow_user_complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, follow_user_error);
			loader.load(request);
			
			//CursorManager.setBusyCursor();	
		}
		
		public function follow_user_complete(e:Event):void
		{
			if (Application.application.extended_user.follow_status_option=="Follow")
			{
				Application.application.extended_user.follow_status_option="Unfollow";
			}
			else
			{
				Application.application.extended_user.follow_status_option=="Follow"
			}
		}
		
		private function follow_user_error(event:IOErrorEvent):void {
			 Alert.show("ioErrorHandler: " + event.toString());
		}
		

	}
}